import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Random;
public class XOREffect extends Canvas{
	private GraphicsMIDlet midlet;
	private Image buf_img, buf_color_img;
	private Graphics buf_graphics, buf_color_graphics;
	private Random random;
	private int draw_pos_x, draw_pos_y;
	private int random_shape = 0;
	private int random_red_color_buf = 0;
	private int random_green_color_buf = 0;
	private int random_blue_color_buf = 0;
	private int[] pixelcolor;
	private int[] tempixel;
	private int[] filteredcolorarray= new int[4];
	private int[] temparray = new int [4];
	public XOREffect(GraphicsMIDlet midlet){
		this.midlet = midlet;
		buf_img = Image.createImage(getWidth(), getHeight());
		buf_graphics = buf_img.getGraphics();
		buf_color_img = Image.createImage(getWidth(), getHeight());		
		buf_color_graphics = buf_color_img.getGraphics();		
		random = new Random();
		draw_pos_x = getWidth()/2 - 20;
		draw_pos_y = getHeight()/2 - 20;
		pixelcolor = new int[1];		
		tempixel = new int[1];
	}
	public void paint(Graphics g){
		//Set background of two buffer
		// buf_graphics.setColor(0xFF7F7F7F);
		buf_graphics.setColor(0x00000000);
		buf_graphics.fillRect(0, 0, getWidth(), getHeight());
		buf_color_graphics.setColor(0xFF000000);
		buf_color_graphics.fillRect(0, 0, getWidth(), getHeight());
		
		//Fill red rectangle in main buffer
		buf_graphics.setColor(0xFFFF0000);		
		buf_graphics.fillRect(getWidth()/2 - 50, getHeight()/2 - 50, 70, 70);
		
		//Fill new shape on xor buffer
		buf_color_graphics.setColor(random_red_color_buf, random_green_color_buf, random_blue_color_buf);		
		if (random_shape == 0){
			buf_color_graphics.fillRect( draw_pos_x, draw_pos_y, 70, 70);
		}
		else if (random_shape == 1){
			buf_color_graphics.fillRoundRect(draw_pos_x, draw_pos_y, 70, 70, 35, 35);
		}
		else if (random_shape == 2){
			buf_color_graphics.fillTriangle(draw_pos_x, draw_pos_y, draw_pos_x - 70, draw_pos_y + 70, draw_pos_x + 70, draw_pos_y + 50);
		}
		else if (random_shape == 3){
			buf_color_graphics.fillArc(draw_pos_x, draw_pos_y, 70, 70, 0, 270);
		}
		else if (random_shape == 4){
			buf_color_graphics.drawString( "Mr.D.MGD.D",draw_pos_x, draw_pos_y, 0);
		}
		//Xor color 
		XorColor();
		g.drawImage(buf_img, 0, 0, 0);
	}
	protected void keyPressed(int keycode){
		if(keycode == this.KEY_NUM1){
			random_shape = random.nextInt(4);
		}else if(keycode == this.KEY_NUM2){		
		
		}else if(keycode == this.KEY_NUM3){
			random_red_color_buf = random.nextInt(255);
			random_green_color_buf = random.nextInt(255);
			random_blue_color_buf = random.nextInt(255);
		}else if(keycode == this.KEY_NUM4){		
		
		}else if(keycode == this.KEY_NUM5){
			draw_pos_x = random.nextInt(getWidth() - 70);
			draw_pos_y = random.nextInt(getHeight() - 70);
		}else if(keycode == this.KEY_NUM6){
		
		}else if(keycode == this.KEY_NUM7){
				
		}else if(keycode == this.KEY_NUM8){
		
		}else if(keycode == this.KEY_NUM9){
		
		}else if(keycode == this.KEY_NUM0){
		
		}else if(keycode == this.KEY_STAR){
		
		}else if(keycode == this.KEY_POUND){
		
		}	
		repaint();
	}	
	protected void XorColor(){
		for(int i = 0; i < getWidth(); i++){
			for(int j = 0; j < getHeight(); j++){				
				try{
					//buf_color_img.getRGB(pixelcolor, 0, buf_color_img.getWidth(), i, j, 1, 1);
					//buf_img.getRGB(tempixel, 0, buf_img.getWidth(), i, j, 1, 1);

					// buf_color_img.getRGB(pixelcolor, 0, buf_img.getWidth(), getWidth()/2, getHeight()/2, 1, 1);
					// buf_img.getRGB(pixelcolor, 0, buf_img.getWidth(), 1, 1, 1, 1);
				}
				catch(Exception e){
					System.out.println("Have an exception: " + e.toString());
				}				
				filteredcolorarray[0] = (int) ((pixelcolor[0]&0xFF000000)>>>24);		
				filteredcolorarray[1] = (int) ((pixelcolor[0]&0x00FF0000)>>>16);		
				filteredcolorarray[2] = (int) ((pixelcolor[0]&0x0000FF00)>>>8);		
				filteredcolorarray[3] = (int) (pixelcolor[0]&0x000000FF);
				if((filteredcolorarray[1]!=0)&&(filteredcolorarray[2]!=0)&&(filteredcolorarray[3]!=0)){
					temparray[0] = (int) ((tempixel[0]&0xFF000000)>>>24);		
					temparray[1] = (int) ((tempixel[0]&0x00FF0000)>>>16);		
					temparray[2] = (int) ((tempixel[0]&0x0000FF00)>>>8);		
					temparray[3] = (int) (tempixel[0]&0x000000FF);
					//Begin xor color 
					temparray[1] = temparray[1]^filteredcolorarray[1];
					temparray[2] = temparray[2]^filteredcolorarray[2];
					temparray[3] = temparray[3]^filteredcolorarray[3];		
					buf_graphics.setColor(temparray[1], temparray[2], temparray[3]);
					buf_graphics.fillRect(i,j,1,1);
					//Print out pixel is not black color position
					// if(filteredcolorarray[2]!=0){
					// System.out.println("Alpha ==  " + filteredcolorarray[0]);		
					// System.out.println("Red["+ i +"]["+ j +"]   ==  " + filteredcolorarray[1]);		
					// System.out.println("Green ["+ i +"]["+ j +"] ==  " + filteredcolorarray[2]);		
					// System.out.println("Blue  ==  " + filteredcolorarray[3]);	
					// }	
				}			
			}
		}	
	}
}